package com.quasar.hpatient;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.multidex.MultiDex;

import com.baidu.mobstat.StatService;
import com.bumptech.glide.request.target.ViewTarget;

import com.quasar.hpatient.base.BaseAppliaction;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.custommodel.InspactionBean;
import com.quasar.hpatient.model.litePalmodel.Aerainfodb;
import com.quasar.hpatient.model.litePalmodel.BloodTypeInfodb;
import com.quasar.hpatient.model.litePalmodel.DailyItemInfodb;
import com.quasar.hpatient.model.litePalmodel.DepartMentInfodb;
import com.quasar.hpatient.model.litePalmodel.DiagnoseInfodb;
import com.quasar.hpatient.model.litePalmodel.DoseModedb;
import com.quasar.hpatient.model.litePalmodel.DosePerioddb;
import com.quasar.hpatient.model.litePalmodel.DurationInfodb;
import com.quasar.hpatient.model.litePalmodel.HospitalInfodb;
import com.quasar.hpatient.model.litePalmodel.InspectionDetailInfodb;
import com.quasar.hpatient.model.litePalmodel.InspectionInfodb;
import com.quasar.hpatient.model.litePalmodel.InspectionItemInfodb;
import com.quasar.hpatient.model.litePalmodel.InspectionTypeInfodb;
import com.quasar.hpatient.model.litePalmodel.KidneySourceTypedb;
import com.quasar.hpatient.model.litePalmodel.MedicineInfodb;
import com.quasar.hpatient.model.litePalmodel.PositonInfodb;
import com.quasar.hpatient.model.litePalmodel.TroubleInfodb;
import com.quasar.hpatient.model.medicalmodel.PatientInfoData;
import com.quasar.hpatient.model.operationmodel.AccountInfo;
import com.vise.log.ViseLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import chatui.DemoHelper;
import cn.bmob.sms.BmobSMS;
import cn.jpush.android.api.JPushInterface;
import okhttp3.OkHttpClient;

/**
 * author : 许建宏
 * Date: 2017-9-26
 */


public class MainAppliaction extends BaseAppliaction {

    public static MainAppliaction applicationContext;
    public static boolean openLog = false;//打开log日志

    public static MainAppliaction getInstance() {
        // TODO Auto-generated method stub
        return applicationContext;
    }
    public static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = this;
        LitePal.initialize(this);
        context=this;
        initOkhttp();
        ViewTarget.setTagId(R.id.glide_tag);
        //初始化短信
        BmobSMS.initialize(this, ConstantManger.SMSKEY);
        /*环信*/
        DemoHelper.getInstance().init(context);
        Jpush();
        StatService.setDebugOn(true);

    }


    /**
     * 初始化极光推送
     */
    public void Jpush() {
        JPushInterface.setDebugMode(true);//正式版的时候设置false，关闭调试
        JPushInterface.init(this);
        //建议添加tag标签，发送消息的之后就可以指定tag标签来发送了
        Set<String> set = new HashSet<>();
        set.add("HPatiemt");//名字任意，可多添加几个
        JPushInterface.setTags(this, set, null);//设置标签
    }

    //读取码表信息
    public Map<String, String> MsgList() {
        return ConstantManger.map;
    }
    //读取消息类型
    public Map<String,String> jpshMap(){
        return ConstantManger.mapJpsh;
    }

    /**
     * 初始化okhttp
     */
    public void initOkhttp() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    //保存userCode
    public void setUserCode(String userCode) {
        // TODO Auto-generated method stub
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        preferences.edit().putString("userCode", userCode).commit();
    }

    //读取userCode
    public String getUserCode() {
        // TODO Auto-generated method stub
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        return preferences.getString("userCode", "");
    }

    //保存密码信息
    public void setPwd(String pwd) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        preferences.edit().putString("pwd", pwd).commit();
    }

    //读取密码
    public String getPWd() {
        // TODO Auto-generated method stub
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        return preferences.getString("pwd", "");
    }

    //个人信息
    public PatientInfoData getPatient() {
        PatientInfoData u = AccountInfo.loadAccount(this);
        return u;
    }

    //保存账号
    public void setUserName(String userName) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        preferences.edit().putString("userName", userName).commit();
    }

    //读取账号
    public String getUserName() {
        // TODO Auto-generated method stub
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        return preferences.getString("userName", "");
    }
    //获取医院信息
    public List<HospitalInfodb> getHospitalInfodbList(){
        return DataSupport.findAll(HospitalInfodb.class);
    }
    //获取不适时长
    public List<DurationInfodb> getDurationinfoList(){

        return DataSupport.findAll(DurationInfodb.class);
    }
    //不适症状信息
    public List<TroubleInfodb> getTroubleInfoList(){
        return DataSupport.findAll(TroubleInfodb.class);
    }
    //检查单信息
    public List<InspectionInfodb> getInspectionInfoList(){
        return DataSupport.where("status=1").order("sort asc").find(InspectionInfodb.class);
    }
    //检查单检查项目信息
    public List<InspectionItemInfodb> getInspectionItemInfoList(){
        return DataSupport.findAll(InspectionItemInfodb.class);
    }


    public List<InspectionDetailInfodb> getInspectionDetailInfodb(String inspectionId) {
        return DataSupport.where("inspectionId=?", inspectionId).order("sort asc").find(InspectionDetailInfodb.class);
    }

    public List<InspectionItemInfodb> getinspectionItemInfodbs(String primaryid) {
        return DataSupport.where("primaryid =? and status=1", primaryid).find(InspectionItemInfodb.class);
    }


    //查询药品信息
    public List<MedicineInfodb> getMedicineInfoList(){
        return DataSupport.findAll(MedicineInfodb.class);
    }
    //服药方式
    public List<DoseModedb> getDoseModeList(){
        return DataSupport.findAll(DoseModedb.class);
    }
    //服药周期信息
    public List<DosePerioddb> getDosePeriodList(){
        return DataSupport.findAll(DosePerioddb.class);
    }
    //日常检查项目信息
    public List<DailyItemInfodb> getDailyItemInfoList(){
        return DataSupport.where("status=1").order("name asc").find(DailyItemInfodb.class);
    }

    //查询地理信息
    public List<Aerainfodb> getAerainfoList(){
        return DataSupport.findAll(Aerainfodb.class);
    }

    //血型信息
    public List<BloodTypeInfodb> getBloodTypeInfoList(){

        return DataSupport.findAll(BloodTypeInfodb.class);
    }
    //器官来源
    public List<KidneySourceTypedb> getKidneySourceTypeList(){
        return DataSupport.findAll(KidneySourceTypedb.class);
    }
    //获取所有省级地区信息
    public List<Aerainfodb> Geography(){
        return DataSupport.where("pid=?","1").find(Aerainfodb.class);
    }
    //获取职位信息
    public  List<PositonInfodb> getPositonInfoList(){
        return DataSupport.findAll(PositonInfodb.class);
    }
    //获取科室信息
    public List<DepartMentInfodb> getDepartMentInfoList(){
        return DataSupport.findAll(DepartMentInfodb.class);
    }

    //保存本地化验单的数据
    List<InspactionBean> inspactionBeanListDB;
    public List<InspactionBean> getInspactionBeanListDB() {
        return inspactionBeanListDB;
    }

    public void setInspactionBeanListDB(List<InspactionBean> inspactionBeanListDB) {
        this.inspactionBeanListDB = inspactionBeanListDB;
    }

}
